package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.*;
import com.wavercaz.freeforum.pojo.vo.PostReplyVo;
import com.wavercaz.freeforum.services.CommentService;
import com.wavercaz.freeforum.services.PostReplyService;
import com.wavercaz.freeforum.services.PostService;
import com.wavercaz.freeforum.utils.JWTUtil;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PostReplyController {
    @Autowired
    private  PostReplyService postReplyService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private PostService postService;

    // 增
    @PutMapping("/postReply")
    @Transactional
    public ResponseResult save(@RequestHeader("Authorization") String token, @RequestBody PostReply postReply) {
        Integer userId = Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString());
        postReply.setUserId(userId);
        postReply.setIsFirst(postReply.getIsFirst() == null?0: postReply.getIsFirst());
        boolean result = postReplyService.save(postReply);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "新增失败");
        else return new ResponseResult(HttpStatus.OK.value(), "新增成功");
    }

    // 删
    @DeleteMapping("/postReply")
    @Transactional
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = postReplyService.removeByIds(ids);
        ids.forEach(v -> {
            commentService.remove(new QueryWrapper<Comment>().eq("post_reply_id",v));
        });
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/postReply")
    @Transactional
    public ResponseResult update(@RequestBody PostReply postReply) {
        boolean result = postReplyService.updateById(postReply);
        if (postReply.getIsFirst() == 1) {
            Post post = postService.getOne(new QueryWrapper<Post>().eq("id", postReply.getPostId()));
            post.setContent(postReply.getContent());
            postService.updateById(post);
        }
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "修改失败");
        else return new ResponseResult(HttpStatus.OK.value(), "修改成功");
    }

    // 查
    @GetMapping("/postReply/{id}")
    public ResponseResult get(@PathVariable Integer id ) {
        PostReply postReply = postReplyService.getById(id);
        if(postReply == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), postReply);
    }

    // 列表
    @PostMapping("/postReplyList")
    public ResponseResult list(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if(commonSearchParam != null) {
            QueryWrapper queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= postReplyService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= postReplyService.list(queryWrapper);
        }
        else data= postReplyService.list();
        return new ResponseResult(HttpStatus.OK.value(), data);
    }

    // Vo列表
    @PostMapping("/postReplyVoList")
    public ResponseResult listVo(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        IPage<PostReplyVo> data = postReplyService.getPostReplyVoList(commonSearchParam, new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()));
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
